<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Mcard extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }
    //todo 激活时间 服务更改 已经激活的不会变。不可删除。
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['is_del', '=', 0];

            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=',  $filter['id'] ];
                }
                if (isset($filter['status'])) {
                    $where[] = ['status', '=',  $filter['status'] ];
                }
                if (isset($filter['name'])) {
                    $where[] = ['name', 'like',  '%'.$filter['name'].'%' ];
                }
            }

            $_list = Db::name('mcard')
                ->page($page, $limit)
                ->where($where)
                ->order('sort desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['count'] = Db::name('mcard_list')->where(['mcard_id' => $v['id']])->count();
                $_list[$k]['count1'] = Db::name('mcard_list')->where(['mcard_id' => $v['id'], 'status' => 0])->count();
            }
            $total = Db::name('mcard')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('mcard')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('mcard')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['create_time'] = time();
            $stmt = Db::name('mcard')->insertGetId($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $stmt = Db::name('mcard')->update($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('mcard')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function server_list()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            foreach ($data['server_data'] as $k => $v) {
                if ($v['server_num'] > 0) {
                    Db::name('mcard_server')
                        ->where(['mcard_id' => $data['mcard_id'], 'server_id' => $k])
                        ->delete();
                    Db::name('mcard_server')->insertGetId([
                        'mcard_id' => $data['mcard_id'],
                        'server_id' => $k,
                        'num' => $v['server_num'],
                        'create_time' => time(),
                    ]);
                }
            }
            $this->success('操作成功！');
        }
        $server_list = Db::name('server')->where(['is_del' => 0])->order('sort desc')->select();
        foreach ($server_list as $k => $v) {
            $server_list[$k]['server_num'] = 0;
            $mcard_serverc = Db::name('mcard_server')->where(['server_id' => $v['id'], 'mcard_id' => $id])->find();
            if ($mcard_serverc) {
                $server_list[$k]['server_num'] = $mcard_serverc['num'];
            }
        }
        $this->assign([
            'mcard_id' => $id,
            'server_list' => $server_list
        ]);
        return $this->fetch('mcard/server');
    }

    //会员卡列表
    public function mcard_list()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            list($page, $limit) = $this->buildTableParames();
            $where = array();
            $where[] = ['is_del', '=', 0];
            $where[] = ['mcard_id', '=', $data['mcid']];
            $_list = Db::name('mcard_list')
                ->page($page, $limit)
                ->where($where)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['status_time']) {
                    $_list[$k]['status_time'] = date('Y-m-d H:i:s', $v['status_time']);
                }
            }
            $total = Db::name('mcard_list')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'mcid' => $id
        ]);
        return $this->fetch('cindex');
    }

    //增加会员卡
    public function mcard_add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            if ($data['count'] > 200) {
                $this->error('生成数量不能大于200！');
            }
            for ($j = 1; $j <= $data['count']; $j++) {
                $is_no = 1;
                while ($is_no) {
                    $no = generateRandomString();
                    $is_no = Db::name('mcard_list')->where('no', '=', $no)->count();
                    $is_no = $is_no > 0 ? 1 : 0;
                }
                $insert_data = array(
                    'mcard_id' => $data['mcid'],
                    'no' => $no,
                    'secret' => generateRandomString(8),
                    'create_time' => time()
                );
                Db::name('mcard_list')->insertGetId($insert_data);
            }
            $this->success('操作成功！');
        }
        $mcid = $this->request->param('mcid/d', 0);
        $this->assign([
            'mcid' => $mcid
        ]);
        return $this->fetch('mcard_add');
    }
    public function activation(){
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $member=Db::name('member')->find($data['member_id']);
            if (!$member){
                $this->error('用户不存在！');
            }
            activeCard($data['id'],$data['member_id']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $this->assign('id', $id);
        return $this->fetch('active');
    }

    public function desc_list()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            list($page, $limit) = $this->buildTableParames();
            $where = array();
            $where[] = ['mcid', '=', $data['mcid']];
            $_list = Db::name('mcard_desc')
                ->page($page, $limit)
                ->where($where)
                ->order('sort desc')
                ->select();
            $total = Db::name('mcard_desc')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'mcid' => $id
        ]);
        return $this->fetch('descindex');
    }
    //增加详情说明
    public function addDesc()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            Db::name('mcard_desc')->insertGetId($data['row']);
            $this->success('操作成功！');
        }
        $mcid = $this->request->param('mcid/d', 0);
        $this->assign([
            'mcid' => $mcid
        ]);
        return $this->fetch('desc_add');
    }
    //编辑详情说明
    public function editDesc()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $stmt = Db::name('mcard_desc')->update($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('mcard_desc')->find($id);
        $this->assign('data', $data);
        return $this->fetch('desc_edit');
    }
    //删除详情说明
    public function delDesc()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            foreach ($ids as $id) {
                Db::name('mcard_desc')->where('id', $id)->delete();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }
}